package com.web;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.apache.struts.upload.FormFile;

import com.alibaba.common.lang.StringUtil;
import com.comm.db.dao.RCvDAO;
import com.comm.db.jdbc.DateUtil;
import com.comm.db.pojo.RCv;
import com.comm.utils.MyResumeDaoUtil;
import com.web.auth.HttpSessionUtil;
import com.web.auth.entity.SysUserBean;
import com.web.vo.StaticFiled;

public class WebFileUtil {
	public synchronized static String saveFile(FormFile file, HttpServletRequest request,
			String resumeid) throws FileNotFoundException, IOException {
		SysUserBean loginUser = HttpSessionUtil.getLoginUser(request);
		if (loginUser == null) {
			return "login";
		}
		int sysuserid = 0;
		try {
			sysuserid = Integer.parseInt(loginUser.getUserId());
		} catch (Exception e) {
			return "login";
		}
		String fileName = file.getFileName();

		fileName = StringUtil.substring(fileName, 0, fileName.lastIndexOf("."));

		File destFileDoc = new File(getDocFullPath(fileName, resumeid));
		save(destFileDoc, file, StaticFiled.FILE_TYPE_DOC, resumeid, sysuserid);

		File destFileHtml = new File(getHtmlFullPath(fileName, resumeid));
		save(destFileHtml, file, StaticFiled.FILE_TYPE_HTML, resumeid,
				sysuserid);

		return getHtmlFullPath(fileName, resumeid);
	}

	public static String getDocFullPath(String fileName, String resumeid) {
		Date date = new Date();
		return StaticFiled.BASE_PATH + "//" + "resumeid" + resumeid + "//"

		+ fileName + DateUtil.convertDate2String(date, "yyyyMMdd-HHmmss")
				+ StaticFiled.FILE_TYPE_DOC;
	}

	public static String getHtmlFullPath(String fileName, String resumeid) {
		Date date = new Date();
		return StaticFiled.BASE_PATH + "//" + "resumeid" + resumeid + "//"
				+ fileName
				+ DateUtil.convertDate2String(date, "yyyyMMdd-HHmmss")
				+ StaticFiled.FILE_TYPE_HTML;
	}

	public static String getFullPath(String fileType, String fileName,
			String resumeid) {
		if (StringUtil.equals(fileName + StaticFiled.FILE_TYPE_HTML, fileType)) {
			return getHtmlFullPath(fileName, resumeid);
		}
		return getDocFullPath(fileName, resumeid);
	}

	private static int save(File destFile, FormFile file, String fileType,
			String resumeid, int sysuserid) throws FileNotFoundException,
			IOException {
		FileUtils.writeByteArrayToFile(destFile, file.getFileData());
		// save to db
		RCvDAO dao = new RCvDAO();
		List<RCv> list = dao.findByResumeid(Integer.parseInt(resumeid));
		RCv cv = new RCv();
		if (list != null)
			for (RCv cvTemp : list) {

				if (cvTemp != null
						&& StringUtil.equals(fileType, cvTemp.getTag())) {
					cv = cvTemp;
				}
			}

		dao.save(cv);
		cv.setFilename(destFile.getName());
		cv.setFilepath(destFile.getPath());

		cv.setResumeid(StringUtil.isBlank(resumeid) ? 0 : Integer
				.parseInt(resumeid));
		cv.setTag(fileType);
		cv.setFilesize(destFile.getTotalSpace() + "");
		dao.saveOrUpdate(cv);
		MyResumeDaoUtil.saveOrUpdate(Integer.parseInt(resumeid), cv.getCvid(), sysuserid);
		return cv.getCvid();
	}

	
 
	public static void main(String[] args) {
		String fileName = "sss-sss.doc";
		fileName = StringUtil.substring(fileName, 0, fileName.lastIndexOf("."));
		Date date = new Date();
		File file = new File("c:\\" + fileName
				+ DateUtil.convertDate2String(date, "yyyy-MM-dd-HH-mm-ss")
				+ ".txt");
		System.out.println(fileName
				+ DateUtil.convertDate2String(date, "yyyyMMdd-HHmmss"));

	}
}
